//
//  XCCollectionCellForFind.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCollectionCellForFind.h"
#import "NetworkingHandlerOfBlock.h"
#import "XCModelForFind.h"
#import "XCCellForScroller.h"
#import "XTTableViewCell.h"
#import "XTCellForeveryone.h"

#define WIDGHT  self.contentView.frame.size.width
#define HEIGHT  self.contentView.frame.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667



@interface XCCollectionCellForFind ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;

@end


@implementation XCCollectionCellForFind

- (instancetype)initWithFrame:(CGRect)frame {
    
    
    self = [super initWithFrame:frame];
    
    if (self) {
       
        [self handleData];
        [self createTableView];
        
    }
    return self;
    

}

- (void)handleData {

    NSString *url = @"http://api.dongting.com/frontpage/frontpage?location=0&version=1458562426544&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=71ae591d23e3955faffe1f13002174549b820ffe&idfa=766C509A-D4C4-4D11-89EA-A316DCF2B7CC&utdid=Vo%2FO9IPtv1UDAHSbJQCXwF%2F6&alf=201200&bundle_id=com.ttpod.music&latitude=38.88245&longtitude=121.5396";
    
   [NetworkingHandler networkingWithUrl:url completeHandle:^(NSURLResponse *response, NSData *data, id result) {
      
       self.arr = [NSMutableArray array];
       NSArray *dataArr= [result objectForKey:@"data"];
       
       for (NSDictionary *dic in dataArr) {
           
           NSArray *arrData = [dic objectForKey:@"data"];
           NSMutableArray *arrayIN = [NSMutableArray array];
           
           for (NSDictionary *dicData in arrData) {
               
               XCModelForFind *model = [[XCModelForFind alloc] init];
               
               [model setValuesForKeysWithDictionary:dicData];
               
               [arrayIN addObject:model];
              
               
           }
        
           [self.arr addObject:arrayIN];
           
       }
       
       
       [self.tableView reloadData];
       
   }];
 
}


- (void)createTableView {

//    self.imageArr = @[@"placehodel2", @"placehodel"];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
//    XCScrollerView *scrolle = [[XCScrollerView alloc] initWithFrame:CGRectMake(0, 0, WIDGHT, HEIGHT / 4) images:self.imageArr];
// 
//    
//    self.tableView.tableHeaderView = scrolle;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"XCCellForScroller" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"poolForScroller"];
    
    
    UINib *nibCell = [UINib nibWithNibName:@"XTTableViewCell" bundle:[NSBundle mainBundle]];
    
    [self.tableView registerNib:nibCell forCellReuseIdentifier:@"poolForXTTab"];

    
    [self.tableView registerClass:[XTCellForeveryone class] forCellReuseIdentifier:@"poolForEveryone"];
    
    
   // [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool3"];
    
    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arr.count;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 || section == 1) {
    
        return 1;
        
    } else {
        
    NSArray *array = [self.arr objectAtIndex:section];
    
   // NSLog(@"%ld", array.count);
    return array.count;
    }
        
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (indexPath.section == 0) {
        
        
        XCCellForScroller *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForScroller"];
        
        
        [cell passModel:[self.arr objectAtIndex:0]];
        
        
        return cell;
        
        
    } else if (indexPath.section == 1) {
    
        XTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForXTTab"];
        
        
        return cell;
    
    
    } else if (indexPath.section == 2) {
    
        XTCellForeveryone *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForEveryone"];
        
        
        
        return cell;
    
    
    
    }
    
    
    else {
     
        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    
    return cell;
        
    }
        
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    if (indexPath.section == 0) {
    
        return height / 4;
    
    } else if (indexPath.section == 1) {
    
        return height / 9;
    
    }
    else  {
    
    
        return height / 10 * 3;
    
    }
    
    
}





@end
